<?php
namespace app\index\controller;

class Index extends Base
{
    public function index()
    {
        $type = new \app\index\model\Types();
        $shop = new \app\index\model\Shop();
        $user = new \app\index\model\User();
        $this->assign("select_tab","tab_game");
        $this->assign("cahngci",$type->getTypes2());
        $this->assign("shops",$shop->getShopAll());
        $users = $user->getUserByUserId($this->uid);
        $users['money'] = round($users['money']/100,2);
        $this->assign("users",$users);
        $this->assign("xia",$type->get_min_and_max());
        $this->assign('pushhost',config('pushhost'));
        return view();
    }
    public function history(){
        
        $jc = new \app\index\model\Jc();
        $res = $jc->getJcAllHis(1);
        
        $this->assign("select_tab","tab_history");
        $this->assign("page",1);
        $this->assign("data",$res);
        return view();
    }
    public function kefu(){

        $shop = new \app\index\model\Shop();
        $user = new \app\index\model\User();
        $this->assign("select_tab","tab_game");
        $this->assign("shops",$shop->getShopAll());
        $users = $user->getUserByUserId($this->uid);
        $users['money'] = round($users['money']/100,2);
        $this->assign("users",$users);
        $this->assign("select_tab","tab_kefu");
        $qr = new \app\index\model\Qr();
        $qrres = $qr->getQr();
        $this->assign("qr",config('httpurl').($qrres['qr']));
        return view();
    }
    public function me(){

        $shop = new \app\index\model\Shop();
        $user = new \app\index\model\User();
        $yj = new \app\index\model\Yongjin();
        $this->assign("select_tab","tab_game");
        $this->assign("shops",$shop->getShopAll());
        $users = $user->getUserByUserId($this->uid);
        $users['money'] = round($users['money']/100,2);
        $this->assign("users",$users);
        $this->assign("yj",$yj->getYongjinNo($this->uid));
        $this->assign("select_tab","tab_me");
        $qr = new \app\index\model\Qr();
        $qrres = $qr->getQr();
        $this->assign("qr",config('httpurl').($qrres['qr']));
        return view();
    }
    function mytuijian(){
        $user = new \app\index\model\User();
        
        $data = $user->getMyTuiJianAll($this->uid);
        $this->assign('data1',$data[0]);
        $this->assign('data2',$data[1]);
        $this->assign('data3',$data[2]);
        return view();
    }
    function payhistory(){
        $pay = new \app\index\model\Payhistory();
    
        $data = $pay->getHistoryAll($this->uid);
        $this->assign('data',$data);
        $this->assign('page',1);
        return view();
    }
    function duihuan(){
        $pay = new \app\index\model\Duihuan();
    
        $data = $pay->getHistoryAll($this->uid);
        $this->assign('data',$data);
        $this->assign('page',1);
        return view();
    }
    function yjhistory(){
        $yj = new \app\index\model\Yongjin();
    
        $data = $yj->getYongjinAll2($this->uid);
        $this->assign('data',$data);
        $this->assign('page',1);
        return view();
    }
    function jchistory(){
        $jc = new \app\index\model\Jc();
    
        $data = $jc->getMyJCAll($this->uid);
        $this->assign('data',$data);
        $this->assign('page',1);
        return view();
    }
    
    public function daili()
    {
        $hb = new \app\index\model\User();
        $res = $hb->getUserByUserId($this->uid);
    
        $qr = "";
        $bg = "";
        $isdaili=0;
        $yong = [];
        if ($res['is_daili']==1){
            $isdaili=1;
            if($res['is_usedefault']==1){
                $yong = $hb->getYongjinInfoD();
            }else{
                $yong = $hb->getYongjinInfoD(false,$this->uid);
            }
        }else{
            $yong = $hb->getYongjinInfoP();
        }
        $yong = array_sort($yong, "type",true);
        //二维码
        $qr=$this->buildQr($res);
        //二维码背景图，暂时没有
        $this->assign('qr',$qr);
        $this->assign('isdaili',$isdaili);
        $this->assign("select_tab","tab_daili");
        $jlres = $hb->getJiangli();
        $this->assign("jiangli",$jlres);
        $this->assign("httpurl",config("httpurl"));
        $this->assign("yongjin",$yong);
        return view();
    }
    
    function buildQr($userInfo){
        include_once EXTEND_PATH. "phpqrcode/phpqrcode.php";
        $name="share.png";
        $path=DS."uploads".DS.'user'.$userInfo['share_id'].DS.'shareqr';
        $uploadpath=ROOT_PATH.DS.'public'.$path;
        if (! is_dir($uploadpath)) {
            mkdir($uploadpath, 0777, true);
        }
        delFileUnderDir($uploadpath);
        $value=$this->getMyShareUrl($userInfo);
    
        $errorCorrectionLevel = "L";
    
        $matrixPointSize = "4";
        \QRcode::png($value, $uploadpath.DS.$name, $errorCorrectionLevel, $matrixPointSize);
        return config('httpurl').$path.DS.$name;
    }
    
    function getMyShareUrl($userInfo){
        return config('httpurl2').'?shareId='.$userInfo['share_id'];
    }
}
